//  Copyright (c) 2010 Mahesh Sharma,Matt MacDonald
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.

package org.prx.prp;

import org.prx.prp.IRockOnNextGenService;
import org.prx.prp.R;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MediaPlaybackLayout extends LinearLayout {
	private Intent intent;
	protected IRockOnNextGenService mService;
	private Context mContext;
	private boolean isConnected;
	private ServiceConnection mServiceConnection = new ServiceConnection() {	
		
		public void onServiceConnected(ComponentName classname, IBinder obj) {
			isConnected = true;
			mService = IRockOnNextGenService.Stub.asInterface(obj);
			TextView nowPlayingProgramHeaderTextView = (TextView) findViewById(R.id.now_playing_program_header);
			TextView nowPlayingStationHeaderTextView = (TextView) findViewById(R.id.now_playing_station_header);
			ImageButton playPauseImageButton = (ImageButton) findViewById(R.id.play_pause_toggle);
			try {
				if((mService.getProgramTitle()!=null))
				{
						nowPlayingProgramHeaderTextView.setText(mService.getProgramTitle());					
						
					nowPlayingStationHeaderTextView.setText(mService.getStationName());
				}
				
						
				//if(mService.isPlaying())
					//playPauseImageButton.setImageResource(R.drawable.pause_button_control);
				//else
					//playPauseImageButton.setImageResource(R.drawable.play_button_control);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}

		public void onServiceDisconnected(ComponentName classname) {
			isConnected = false;
			mService=null;
		}

	};


	@Override
	public void onLayout(boolean c, int a, int b, int c1, int d) {
		super.onLayout(c, a, b, c1, d);
	}
	BroadcastReceiver mStatusListener = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			String programName = intent.getExtras().getString("PROGRAM_NAME");
			if(programName!=null)
			{
			TextView nowPlayingProgramHeaderTextView = (TextView) findViewById(R.id.now_playing_program_header);
			nowPlayingProgramHeaderTextView.setText(programName);
			TextView nowPlayingStationHeader = (TextView) findViewById(R.id.now_playing_station_header);
			nowPlayingStationHeader.setText(intent.getExtras().getString("STATION_NAME"));
			}
			try {
			    ImageButton playPauseToggle = (ImageButton) findViewById(R.id.play_pause_toggle);
			    /*
				if (action.equals(Constants.PLAYBACK_COMPLETE) || (action.equals(Constants.PLAYSTATE_CHANGED) && mService != null && !mService.isPlaying())) {
					playPauseToggle.setImageResource(R.drawable.play_button_control);
				} else if (action.equals(Constants.PLAYSTATE_CHANGED) && mService != null && mService.isPlaying()) {
                    playPauseToggle.setImageResource(R.drawable.pause_button_control);
				}
				*/
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	};

	
	public MediaPlaybackLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		mContext = context;
		LayoutInflater.from(context).inflate(R.layout.media_playback_bar, this,
				true);
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(Constants.PLAYSTATE_CHANGED);
		intentFilter.addAction(Constants.META_CHANGED);
		intentFilter.addAction(Constants.PLAYBACK_COMPLETE);
		context.registerReceiver(mStatusListener, new IntentFilter(intentFilter));
		intent = new Intent(context.getApplicationContext(), BackgroundPlaybackService.class);		
		context.getApplicationContext().bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
		final ImageButton playPauseToggleButton = (ImageButton) findViewById(R.id.play_pause_toggle);
		/**
		 * Instead of binding to service each time, we will get a message,
		 * getMetaData and it will return the program name and status
		 */

		playPauseToggleButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.setAction(Constants.TOGGLEPAUSE_ACTION);
				mContext.sendBroadcast(intent);
			}
		});
	}

	public void unRegisterListeners() {		
		if (isConnected && mServiceConnection != null) { 
		    mContext.getApplicationContext().unbindService(mServiceConnection);
		    mServiceConnection = null;
		}
		if (mStatusListener != null) {
		    mContext.unregisterReceiver(mStatusListener);
		    mStatusListener = null;
		}
		
	}

}
